package tragame.game;

import java.rmi.RemoteException;
import java.util.ArrayList;

import tragame.game.Building.BuildingType;
import tragame.game.BuildingProd.BuildingProdType;
import tragame.game.BuildingStorage.BuildingStorageType;
import tragame.game.Unit.RaceType;
import tragame.game.Unit.UnitType;
import tragame.interfaces.IBuilding;
import tragame.interfaces.IBuildingProd;
import tragame.interfaces.IUnit;
import tragame.interfaces.IVillage;

/**
 * This class describes a village which is a square on the map.
 * 
 * @author jingjing
 * 
 */
public class Village extends Square implements IVillage {

	private static final long serialVersionUID = 1L;
	
	private final int nbBuildings = 9;
	private String player;
	private String name;
	private String race;
	private Building[] buildings = new Building[nbBuildings];
	private ArrayList<Unit> units;
	private int curWood;
	private int curSoil;
	private int curIron;
	private int curFood;
	private int maxWood;
	private int maxSoil;
	private int maxIron;
	private int maxFood;
	private int foodConsomation;

	/**
	 * constructor : create a village with one worker inside
	 * 
	 * @param name
	 *            : name of the village
	 * @param f
	 *            : field that has to be replaced
	 * @throws RemoteException 
	 */
	public Village(String player, String name, String race, Field f) throws RemoteException {
		
		super(f.getPosX(), f.getPosY());
		
		this.curWood = 1000;
		this.curIron = 1000;
		this.curFood = 1000;
		this.curSoil = 1000;
		this.maxWood = 1000;
		this.maxIron = 1000;
		this.maxFood = 1000;
		this.maxSoil = 1000;
		this.buildings[0] = new Building(BuildingType.Hotel);
		this.buildings[1] = new BuildingProd(BuildingProdType.Ferme);
		this.buildings[2] = new BuildingProd(BuildingProdType.Scierie);
		this.buildings[3] = new BuildingProd(BuildingProdType.CARRIERE);
		this.buildings[4] = new BuildingProd(BuildingProdType.Mine);
		this.buildings[5] = new BuildingStorage(BuildingStorageType.Entrepot);
		this.buildings[6] = new BuildingStorage(BuildingStorageType.Silo);
		this.buildings[7] = new Building(BuildingType.Caserne);
		this.buildings[8] = new Building(BuildingType.Ecurie);
		this.player = player;
		this.name = name;
		this.race = race;
	}

	public void addUnit(UnitType type, RaceType race) throws RemoteException {
		this.units.add(new Unit(type, race));
	}

	public IUnit getUnitAt(int position) {
		return (IUnit)this.units.get(position);
	}

	public void produce() throws RemoteException {
		int produceFood = ((IBuildingProd) this.getBuilding(1)).getRessource()
				.getProduction();
		int oldFood = this.getCurFood();
		this.setCurFood(oldFood + produceFood);
		int produceWood = ((IBuildingProd) this.getBuilding(2)).getRessource()
				.getProduction();
		int oldWood = this.getCurWood();
		this.setCurWood(oldWood + produceWood);
		int produceSoil = ((IBuildingProd) this.getBuilding(3)).getRessource()
				.getProduction();
		int oldSoil = this.getCurSoil();
		this.setCurSoil(oldSoil + produceSoil);
		int produceIron = ((IBuildingProd) this.getBuilding(4)).getRessource()
				.getProduction();
		int oldIron = this.getCurIron();
		this.setCurIron(oldIron + produceIron);
	}
	
	public IBuilding getBuilding(int number) {
		return (IBuilding)this.buildings[number];
	}

	/**
	 * get the name of the player owning the village
	 * 
	 * @return the name of the player owning the village
	 */
	public String getPlayer() {
		return player;
	}

	/**
	 * get the name of the village
	 * 
	 * @return the name of the village
	 */
	public String getName() {
		return name;
	}

	/**
	 * get the race of the village
	 * 
	 * @return the race of the village
	 */
	public String getRace() {
		return race;
	}

	/**
	 * get the total food consomation of the village.
	 * 
	 * @return the number of parts
	 */
	public int getFoodConsomation() {
		return this.foodConsomation;
	}

	/**
	 * get the curent amount of food that can be collected
	 * 
	 * @return the curent amount of food
	 */
	public int getCurFood() {
		return this.curFood;
	}

	/**
	 * get the curent amount of ore that can be collected
	 * 
	 * @return the curent amount of ore
	 */
	public int getCurIron() {
		return this.curIron;
	}

	/**
	 * get the curent amount of soil that can be collected
	 * 
	 * @return the curent amount of soil
	 */
	public int getCurSoil() {
		return this.curSoil;
	}

	/**
	 * get the curent amount of wood that can be collected
	 * 
	 * @return the curent amount of wood
	 */
	public int getCurWood() {
		return this.curWood;
	}

	/**
	 * get the maximum amount of food that can be collected
	 * 
	 * @return the maximum amount of food
	 */
	public int getMaxFood() {
		return this.maxFood;
	}

	/**
	 * get the maximum amount of ore that can be collected
	 * 
	 * @return the maximum amount of ore
	 */
	public int getMaxIron() {
		return this.maxIron;
	}

	/**
	 * get the maximum amount of soil that can be collected
	 * 
	 * @return the maximum amount of soil
	 */
	public int getMaxSoil() {
		return this.maxSoil;
	}

	/**
	 * get the maximum amount of wood that can be collected
	 * 
	 * @return the maximum amount of wood
	 */
	public int getMaxWood() {
		return this.maxWood;
	}	

	/**
	 * set a new name to the village
	 * 
	 * @param name
	 *            : name of the village
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * change a village race
	 * 
	 * @param race
	 *            : race of the village
	 */
	public void setRace(String race) {
		this.race = race;
	}

	/**
	 * change the number of food parts for this village
	 * 
	 * @param foodParts
	 *            : new number of food parts
	 */
	public void setFoodConsomation(int foodConsomation) {
		this.foodConsomation = foodConsomation;
	}

	/**
	 * change the maximum amount of food that can be collected
	 * 
	 * @param maxFood
	 *            : new amount of food
	 */
	public void setMaxFood(int maxFood) {
		this.maxFood = maxFood;
	}

	/**
	 * change the maximum amount of ore that can be collected
	 * 
	 * @param maxIron
	 *            : new amount of ore
	 */
	public void setMaxIron(int maxIron) {
		this.maxIron = maxIron;
	}

	/**
	 * change the maximum amount of soil that can be collected
	 * 
	 * @param maxSoil
	 *            : new amount of soil
	 */
	public void setMaxSoil(int maxSoil) {
		this.maxSoil = maxSoil;
	}

	/**
	 * change the maximum amount of wood that can be collected
	 * 
	 * @param maxWood
	 *            : new amount of wood
	 */
	public void setMaxWood(int maxWood) {
		this.maxWood = maxWood;
	}

	/**
	 * get the curent amount of food that can be collected
	 * 
	 * @return the curent amount of food
	 */
	public void setCurFood(int curFood) {
		this.curFood = curFood;
	}

	/**
	 * get the curent amount of ore that can be collected
	 * 
	 * @return the curent amount of ore
	 */
	public void setCurIron(int curIron) {
		this.curIron = curIron;
	}

	/**
	 * get the curent amount of soil that can be collected
	 * 
	 * @return the curent amount of soil
	 */
	public void setCurSoil(int curSoil) {
		this.curSoil = curSoil;
	}

	/**
	 * get the curent amount of wood that can be collected
	 * 
	 * @return the curent amount of wood
	 */
	public void setCurWood(int curWood) {
		this.curWood = curWood;
	}

	/**
	 * @return the buildings
	 */
	public Building[] getBuildings() {
		return buildings;
	}

	/**
	 * @param buildings
	 *            the buildings to set
	 */
	public void setBuildings(Building[] buildings) {
		this.buildings = buildings;
	}

	/**
	 * @return the nbBuildings
	 */
	public int getNbBuildings() {
		return nbBuildings;
	}

	/**
	 * @param player
	 *            the player to set
	 */
	public void setPlayer(String player) {
		this.player = player;
	}
}